package com.jrx.anytxn.reporting.job.xib.config;

import com.jrx.anytxn.customer.entity.CmCustomerFirstLevel;
import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.entity.CoLonCrdtInfoBean;
import com.jrx.anytxn.reporting.job.xib.step.CreditInfoReportingListener;
import com.jrx.anytxn.reporting.job.xib.step.CreditInfoReportingProcessor;
import com.jrx.anytxn.reporting.job.xib.step.CreditInfoReportingWriter;
import com.jrx.anytxn.reporting.job.xib.step.CreditInfoXajReportingProcessor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.support.SynchronizedItemStreamReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * @comment: 贷款授信信息报表任务定义
 * @author: hsoong
 * @date: 2021/7/1 21:26
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class CreditInfoReportingConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;

    @Autowired
    CreditInfoReportingListener creditInfoReportingListener;

    @Autowired
    CreditInfoReportingProcessor creditInfoReportingProcessor;

    @Autowired
    CreditInfoXajReportingProcessor creditInfoXajReportingProcessor;

    @Autowired
    CreditInfoReportingWriter creditInfoReportingWriter;

    @Bean
    public Job xibCreditInfoReportingJob() {
        return jobBuilderFactory.get("xibCreditInfoReportingJob")
                .incrementer(new RunIdIncrementer())
                .listener(creditInfoReportingListener)
                .start(xibCreditInfoReportingJobStep())
                .next(xibCreditInfoXajReportingJobStep())
                .build();
    }

    @Bean
    public Step xibCreditInfoReportingJobStep() {

        return stepBuilderFactory.get("xibCreditInfoReportingJobStep")
                .<CmCustomerFirstLevel, CoLonCrdtInfoBean>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(creditInfoReportingReader())
                .processor(creditInfoReportingProcessor)
                .writer(creditInfoReportingWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    /**
     * @description: 贷款授信信息报表任务定义  (兴安借)
     * @author: shigongtai
     * @date: 2023/4/13 14:29
     */
    @Bean
    public Step xibCreditInfoXajReportingJobStep() {

        return stepBuilderFactory.get("xibCreditInfoReportingJobStep")
                .<CmCustomerFirstLevel, CoLonCrdtInfoBean>chunk(anyTxnBatchProperties.getReportingCustomerConfig().getChunkLimit())
                .reader(creditInfoReportingReader())
                .processor(creditInfoXajReportingProcessor)
                .writer(creditInfoReportingWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getReportingCustomerConfig().getThrottleLimit())
                .build();
    }

    @Bean
    @StepScope
    public SynchronizedItemStreamReader<CmCustomerFirstLevel> creditInfoReportingReader() {
        MyBatisCursorItemReader<CmCustomerFirstLevel> cursorItemReader = new MyBatisCursorItemReader<>();
        cursorItemReader.setSaveState(false);
        cursorItemReader.setSqlSessionFactory(sqlSessionFactory);
        cursorItemReader.setQueryId("com.jrx.anytxn.customer.mapper.ext.ExtCmCustomerFirstLevelMapper.selectAllCust");
        SynchronizedItemStreamReader<CmCustomerFirstLevel> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
        synchronizedItemStreamReader.setDelegate(cursorItemReader);
        return synchronizedItemStreamReader;
    }

}
